<?php
class SkillModel extends Model {
	public function getSkillId($name) {
		$sql = 'SELECT `skillid` FROM `skills` WHERE `title` = :t';
		$this->query($sql, array( 't' => $name));
		return $this->getValue();
	}
	
	public function getSkillLevel($skillid, $uid) {
		$sql = 'SELECT `level` FROM `skill_status` WHERE `skillid` = :sid AND `uid` = :uid';
		$this->query($sql, array( 'sid' => $skillid, 'uid' => $uid));
		if (!$this->hasRows()) {
			$sql = 'INSERT INTO `skill_status` VALUES (:sid, :uid, 0, 1)';
			$this->query($sql, array( 'sid' => $skillid, 'uid' => $uid));
			return 0;
		}
		return $this->getValue();
	}
	
	public function deactivate($skillid, $uid) {
		$sql = 'UPDATE `skill_status` SET `status` = 0 WHERE `skillid` = :sid AND `uid` = :uid';
		$this->query($sql, array( 'sid' => $skillid, 'uid' => $uid));
		return 0;
	}
	
	public function activate($skillid, $uid) {
		$sql = 'UPDATE `skill_status` SET `status` = 1 WHERE `skillid` = :sid AND `uid` = :uid';
		$this->query($sql, array( 'sid' => $skillid, 'uid' => $uid));
		return 0;
	}
	
	public function getSkillStatus($skillid, $uid) {
		$sql = 'SELECT `status` FROM `skill_status` WHERE `skillid` = :sid AND `uid` = :uid';
		$this->query($sql, array( 'sid' => $skillid, 'uid' => $uid));
		return $this->getValue();
	}
	
	public function getAllSkills() {
		$sql = 'SELECT * FROM `skills`';
		$this->query($sql);
		return $this->getRows();
	}
	
	public function setReactivate($skillid, $uid, $cooldown) {
		$q = new SkillQueue();
		$q->skillid = $skillid;
		$q->due = time() + $cooldown*60;
		$q->uid = $uid;
	}
	
	public function reactivateOld() {
		$this->query('SELECT * FROM `skill_queue` WHERE `due` <= :time', array('time'=>time()));
		$a = $this->getRows();
		foreach ($a as $row) {
			$this->activate($row['skillid'], $row['uid']);
		}
	}
}
